<template>
    <div class="home-container">
        <div class="form-box">
            <div class="form-head">
                <img class="form-head-font" src="../../static/images/form/form_head_font.png" alt="" />
            </div>
            <div class="form-content">
                <div class="form-item">
                    <span class="label">您的姓名：</span>
                    <input v-model="username" class="form-input" type="text" placeholder="请输入您的姓名(必填)" />
                </div>
                <div class="form-item">
                    <span class="label">您的性别：</span>
                    <ul class="radio-wrap">
                        <li class="radio-list" @click="changeSex('男')">
                            <i class="iconfont icon-duigouxiao" style="color: #1a3b8c" v-if="sex == '男'"></i>
                            <span class="round" v-else></span>
                            <span class="font">男</span>
                        </li>
                        <li class="radio-list" @click="changeSex('女')">
                            <i class="iconfont icon-duigouxiao" style="color: #1a3b8c" v-if="sex == '女'"></i>
                            <span class="round" v-else></span>
                            <span class="font">女</span>
                        </li>
                    </ul>
                </div>
                <div class="form-item">
                    <span class="label">出生时间：</span>
                    <p @click="showCanendarStatus" class="birth-input">
                        <span v-if="!birthday">请选择出生时间(必填)</span>
                        <span class="font" style="color: #333" v-else>{{ birthday }}</span>
                    </p>
                </div>
            </div>
            <div class="form-bottom">
                <img @click="showPayPopup" class="cesuan-btn" src="../../static/images/form/cesuan_btn.png" alt="" />
                <p class="btn-text">
                    测试部分免费，获取报告或解锁全部内容时，需付费查看
                </p>
            </div>
        </div>
        <div class="bottom-info-card flex items-center justify-between">
            <!-- <div class="info-left flex items-center">
                <i v-if="protocolStatus" class="iconfont icon-gou"
                    style="margin-right: 10rpx; font-size: 33rpx; color: #ffe7a9" @click="changeProtocolStatus"></i>
                <span v-else class="no-check-icon" @click="changeProtocolStatus"></span>
                <span class="font">同意</span>
                <span class="protocol-font" @click="goUserProtocol">用户协议</span>
                <span class="font ml-1 mr-1">和</span>
                <span class="protocol-font" @click="goPrivacyProtocol">隐私协议</span>
            </div> -->
            <div class="info-right">
                <!-- <span class="protocol-font" @click="goHistoryOrder">查询历史订单</span> -->
            </div>
        </div>
        <better-calendar ref="betterCalendarRef" :showCalendar="showCalendar" @confirm="calendarConfirm"
            @cancel="showCalendar = false"></better-calendar>
        <!-- 价格弹窗 -->
        <u-popup v-model="payPopup" mode="bottom" :border-radius="12" closeable>
            <div class="pay-popup">
                <div class="pay-head">
                    <!-- <div class="origin-price">原价：￥{{ originPrice }}</div> -->
                    <div class="sell-price">
                        <span class="font">结缘价:</span>
                        <span class="price-font">￥{{ sellPrice }}</span>
                    </div>
                </div>
                <div class="pay-method flex items-center justify-between">
                    <div class="wechat-wrap flex items-center">
                        <img class="wechat-icon" src="../../static/images/form/wechat-icon.png" alt="" />
                        <span class="font">微信支付</span>
                    </div>
                    <div class="pay-round"></div>
                </div>
                <div class="pay-button" @click="createOrder">立即解锁内容</div>
            </div>
        </u-popup>
    </div>
</template>
<script setup lang="ts">
import { ref } from 'vue';
import BetterCalendar from '@/components/better-calendar/index.vue';
import * as accountApi from '@/api/account';
import wechatOa from '@/utils/wechat'

const username = ref('');
const sex = ref("男"); // 0女 1男
const birthday = ref('');
const time = ref(""); // 时辰

const showCalendar = ref(false); // 日历弹窗状态
const protocolStatus = ref(false); // 协议状态
const payPopup = ref(false); // 价格弹窗
const originPrice = '138';
const sellPrice = ref("");

// 获取价格
const getPriceFn = async () => {
    const res = await accountApi.getPrice();
    if (res) {
        sellPrice.value = res.price;
    }
}
getPriceFn();

// 展示日历
const showCanendarStatus = () => {
    showCalendar.value = true;
};

// 日历确认
const calendarConfirm = (data: any) => {
    birthday.value = data.date;
    time.value = data.time;
    showCalendar.value = false;
};

// 切换性别
const changeSex = (value: any) => {
    sex.value = value;
};

// 切换协议
const changeProtocolStatus = (status: any) => {
    protocolStatus.value = !protocolStatus.value;
};

const showPayPopup = () => {
    if (!username.value) {
        uni.showToast({ title: '请输入姓名', icon: 'none' });
        return false;
    }
    if (!birthday.value) {
        uni.showToast({ title: '请选择出生时间', icon: 'none' });
        return false;
    }
    payPopup.value = true;
};

const goUserProtocol = () => { }

const goPrivacyProtocol = () => { }

// 创建订单
const createOrder = async () => {
    const content = `姓名:${username.value},性别:${sex.value},出生时间:${birthday.value},时辰:${time.value}`;
    const res = await accountApi.createOrder({
        content
    });
    prepay(res);
};

// 发起支付
const prepay = async (data: any) => {
    try {
        await wechatOa.pay({
            timeStamp: data.config.timeStamp,
            nonceStr: data.config.nonceStr,
            package: data.config.package,
            signType: data.config.signType,
            paySign: data.config.paySign,
        })
        uni.navigateTo({
            url: "/pages/order/success?order_id=" + data.pay_id
        })
    } catch (e) {
        uni.showToast({ title: "支付失败", icon: 'none' });
    }
}
</script>
<style lang="scss" scoped>
.home-container {
    min-height: calc(100vh - 80rpx);
    background: #51639d;
    padding-top: 80rpx;

    .form-box {
        @apply box-border relative;
        width: 710rpx;
        height: 640rpx;
        background: #f4f9ff;
        margin: 0 20rpx;
        padding: 80rpx 20rpx 20rpx 20rpx;
        border: 3px solid #8fa7e4;
        border-radius: 16rpx;

        .form-head {
            @apply flex items-center justify-center absolute;
            width: 640rpx;
            height: 124rpx;
            background: url('../../static/images/form/form_head.png') no-repeat;
            background-size: 100% 100%;
            top: -64rpx;
            left: 50%;
            transform: translate(-50%);

            .form-head-font {
                width: 342rpx;
                height: 38rpx;
            }
        }

        .form-content {
            .form-item {
                @apply flex items-center overflow-hidden box-border;
                background-color: #dfecfd;
                margin-bottom: 32rpx;
                height: 80rpx;
                line-height: 80rpx;
                border-radius: 40rpx;
                color: #efefff;

                .label {
                    color: #333;
                    padding-left: 32rpx;
                    font-size: 32rpx;
                }

                .form-input {
                    background: transparent;
                    border: none;
                    color: #333;
                    font-size: 32rpx;
                    flex: 1;
                    padding-right: 32rpx;
                    margin-left: 20rpx;
                }

                .radio-wrap {
                    @apply flex items-center list-none;
                    padding-left: 20rpx;
                    color: #333;

                    .radio-list {
                        @apply flex items-center;

                        .round {
                            @apply inline-block;
                            width: 30rpx;
                            height: 30rpx;
                            border-radius: 50%;
                            border: 1px solid #1a3b8c;
                        }

                        .font {
                            color: #333;
                            font-size: 32rpx;
                            margin-left: 6px;
                        }

                        &:first-child {
                            margin-right: 20px;
                        }
                    }
                }

                .birth-input {
                    @apply whitespace-nowrap overflow-hidden inline-block cursor-pointer box-border;
                    color: #787c95;
                    background: transparent;
                    flex: 1;
                    text-align: left;
                    font-size: 32rpx;
                    padding-left: 20rpx;
                }
            }
        }

        .form-bottom {
            .cesuan-btn {
                width: 658rpx;
                height: 133rpx;
                animation: btnScale 0.5s linear alternate infinite;
            }

            .btn-text {
                color: rgb(153, 153, 153);
                opacity: 0.7;
                font-size: 24rpx;
                text-align: center;
            }

            @keyframes btnScale {
                0% {
                    transform: scale(0.9);
                }

                100% {
                    transform: scale(1);
                }
            }
        }
    }

    .bottom-info-card {
        @apply box-border;
        color: #fff;
        font-size: 26rpx;
        padding: 0 24rpx;
        margin-top: 30rpx;

        .protocol-font {
            color: #ffe7a9;
            border-bottom: 1px solid #ffe7a9;
        }

        .no-check-icon {
            @apply flex items-center justify-center;
            width: 28rpx;
            height: 28rpx;
            border-radius: 50%;
            border: 1px solid #ffe7a9;
            color: #ffe7a9;
            margin-right: 10rpx;
        }
    }

    .pay-popup {
        @apply w-full box-border;
        background-color: #fffbd8;
        border-radius: 8rpx 8rpx 0 0;
        z-index: 999;
        height: 500rpx;
        padding: 80rpx 30rpx;

        .pay-head {
            @apply flex items-center;

            .origin-price {
                color: #999;
                text-decoration: line-through;
                height: 40rpx;
                display: flex;
                align-items: center;
                line-height: normal;
                font-size: 28rpx;
            }

            .sell-price {
                @apply inline-block;
                margin-left: 30rpx;
                color: #fff;
                background-color: #c92727;
                border-radius: 6rpx;
                padding: 10rpx 16rpx;
                font-size: 28rpx;

                .font {
                    margin-right: 20rpx;
                }
            }
        }

        .pay-method {
            margin-top: 60rpx;

            .wechat-icon {
                width: 60rpx;
                height: 60rpx;
            }

            .font {
                color: #333;
                margin-left: 16rpx;
            }
        }

        .pay-button {
            background-color: #c92727;
            border-radius: 48rpx;
            color: #fff;
            font-size: 28rpx;
            font-weight: 700;
            text-align: center;
            margin-top: 90rpx;
            padding: 24rpx 0;
            animation: btnScale 0.5s linear alternate infinite;
        }

        .pay-round {
            width: 28rpx;
            height: 28rpx;
            border-radius: 50%;
            background: #29a339;
        }
    }
}
</style>
